package com.organizadorestudiantil;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ControlDB {
	String cadena;
	
	public ControlDB(Context context)
	{
		this.cadena = null;
	}
	
	//geters	
	public void consulta(String cadena, SQLiteDatabase bd)
	{
		this.cadena = cadena;
		bd.execSQL(cadena);
		bd.close();		
	}
	
	public void crearBD(SQLiteDatabase bd){				
		String crearT;	
		crearT = "CREATE TABLE IF NOT EXISTS Curso (Id_curso INTEGER PRIMARY KEY AUTOINCREMENT, Nombre_curso TEXT, Color INTEGER);";
		bd.execSQL(crearT);	 
    	crearT = "CREATE TABLE IF NOT EXISTS Horario (Id_horario INTEGER PRIMARY KEY AUTOINCREMENT, Inicio TIME, Fin TIME);";
    	bd.execSQL(crearT);
    	crearT = "CREATE TABLE IF NOT EXISTS cursoHorario (Id_chora INTEGER PRIMARY KEY AUTOINCREMENT, Dia TEXT, Id_curso INTEGER NOT NULL " +
    			"CONSTRAINT fk_Id_curso REFERENCES Curso (Id_curso) ON DELETE CASCADE, Id_horario INTEGER NOT NULL CONSTRAINT " +
    			"fk_Id_horario REFERENCES Horario (Id_horario) ON DELETE CASCADE);";
    	bd.execSQL(crearT);
    	crearT = "CREATE TABLE IF NOT EXISTS Tarea (Id_tarea INTEGER PRIMARY KEY AUTOINCREMENT, Nombre_tarea TEXT, Tipo TEXT, " +
    			"Fecha DATE, Hora TIME, Prioridad INTEGER, Descripcion TEXT, Id_curso INTEGER NOT NULL CONSTRAINT fk_Id_curso " +
    			"REFERENCES Curso (Id_curso) ON DELETE CASCADE) ";    	
    	bd.execSQL(crearT);
    	crearT = "CREATE TABLE IF NOT EXISTS Alarma (Id_alarma INTEGER PRIMARY KEY AUTOINCREMENT, Hora_recordatorio TIME, " +
    			"Fecha_recordatorio DATE, Id_tarea INTEGER NOT NULL CONSTRAINT fk_Id_tarea REFERENCES Tarea (Id_tarea) ON DELETE " +
    			"CASCADE);";
    	bd.execSQL(crearT);    	
    	llenarHorario(bd);
    	bd.close();    	    	 	    	
	}
	
	private void llenarHorario(SQLiteDatabase bd) {	
		Cursor c= bd.rawQuery("Select * from Horario;",null);
		if (c.getCount() < 1){
			bd.execSQL("INSERT INTO Horario VALUES (null,'07:00','08:30');");
			bd.execSQL("INSERT INTO Horario VALUES (null,'08:30','10:00');");
			bd.execSQL("INSERT INTO Horario VALUES (null,'10:15','11:45');");
			bd.execSQL("INSERT INTO Horario VALUES (null,'11:55','13:25');");
			bd.execSQL("INSERT INTO Horario VALUES (null,'14:30','16:00');");
			bd.execSQL("INSERT INTO Horario VALUES (null,'16:30','18:00');");
			bd.execSQL("INSERT INTO Horario VALUES (null,'18:15','19:45');");
			bd.execSQL("INSERT INTO Horario VALUES (null,'20:00','21:30');");			
		}
	}
	public void llenarTareas(SQLiteDatabase bd, String nombre, String tipo, String fecha, String hora, int prioridad, int idCurso) {
		bd.execSQL("INSERT INTO Tarea VALUES (null, '"+nombre+"' , '"+tipo+"' , '"+fecha+"' , '"+hora+"' , "+prioridad+" , 'nada' , "+idCurso+");");		
	}
	
	public void llenarAlarma(SQLiteDatabase bd,String hora, String fecha)
	{
		int idtarea=0;
		Cursor c=bd.rawQuery("SELECT Id_tarea FROM Tarea ORDER BY Id_tarea DESC", null);
		if(c.moveToFirst())
			{ idtarea=c.getInt(0); }
		bd.execSQL("INSERT INTO Alarma VALUES (null, '"+hora+"' ,'"+fecha+"','"+idtarea+"' );");	
	}
}
